<template>
  <div class="role-selector-container">
    <!-- 动态背景元素 -->
    <div class="dynamic-background">
      <div class="floating-item" v-for="(item, index) in floatingItems" :key="index"
           :style="item.style">
        <el-icon :size="item.size" :color="item.color">
          <component :is="item.icon" />
        </el-icon>
      </div>
    </div>

    <div class="header">
      <h2>开启您的专属体验</h2>
      <p>请选择您的角色，开启专属之旅</p>
    </div>

    <div class="role-cards">
      <el-card
          class="role-card"
          :class="{ 'active': selectedRole === 'merchant' }"
          @click="selectRole('merchant')"
          shadow="hover"
      >
        <div class="card-content">
          <div class="icon-wrapper merchant">
            <el-icon :size="60">
              <Shop />
            </el-icon>
          </div>
          <h3>我是商家</h3>
          <p>开启高效管理之路，轻松管理商品与订单</p>
          <div class="highlight-bar"></div>
        </div>
      </el-card>

      <el-card
          class="role-card"
          :class="{ 'active': selectedRole === 'customer' }"
          @click="selectRole('customer')"
          shadow="hover"
      >
        <div class="card-content">
          <div class="icon-wrapper customer">
            <el-icon :size="60">
              <User />
            </el-icon>
          </div>
          <h3>我是用户</h3>
          <p>享受便捷购物体验，发现更多优质商品</p>
          <div class="highlight-bar"></div>
        </div>
      </el-card>
    </div>

    <el-button
        type="primary"
        size="large"
        @click="confirmRole"
        :disabled="!selectedRole"
        class="confirm-btn"
    >
      立即进入
      <el-icon class="arrow-right"><Right /></el-icon>
    </el-button>
  </div>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';
import { Shop, User, Right, Goods, Present, ShoppingCart, ShoppingBag, Coin } from '@element-plus/icons-vue';
import { useRouter } from 'vue-router';

const router = useRouter();
const selectedRole = ref('');

// 动态背景元素
const floatingItems = ref([]);
const icons = [Goods, Present, ShoppingCart, ShoppingBag, Coin];
const colors = ['#409EFF', '#67C23A', '#E6A23C', '#F56C6C', '#909399'];

// 生成随机浮动元素
const generateFloatingItems = () => {
  const items = [];
  for (let i = 0; i < 12; i++) {
    items.push({
      icon: icons[Math.floor(Math.random() * icons.length)],
      size: Math.floor(Math.random() * 30) + 20,
      color: colors[Math.floor(Math.random() * colors.length)],
      style: {
        left: `${Math.random() * 100}%`,
        top: `${Math.random() * 100}%`,
        animationDuration: `${Math.random() * 10 + 10}s`,
        animationDelay: `${Math.random() * 5}s`,
        opacity: Math.random() * 0.5 + 0.3
      }
    });
  }
  return items;
};

// 动画帧
let animationFrame;
const animateBackground = () => {
  floatingItems.value.forEach((item, index) => {
    const currentTop = parseFloat(item.style.top);
    const newTop = (currentTop + 0.05) % 100;
    item.style.top = `${newTop}%`;

    // 轻微左右摆动
    if (index % 2 === 0) {
      const currentLeft = parseFloat(item.style.left);
      const newLeft = currentTop + Math.sin(Date.now() / 1000 + index) * 0.5;
      item.style.left = `${newLeft}%`;
    }
  });
  animationFrame = requestAnimationFrame(animateBackground);
};

onMounted(() => {
  floatingItems.value = generateFloatingItems();
  animateBackground();
});

onUnmounted(() => {
  cancelAnimationFrame(animationFrame);
});

const selectRole = (role) => {
  selectedRole.value = role;
};

const confirmRole = () => {
  if (selectedRole.value === 'merchant') {
    router.push({ path: '/login2', query: { role: 'merchant' } });
  } else if (selectedRole.value === 'customer') {
    router.push('/login');
  }
};
</script>

<style scoped>
body, html, .role-selector-container {
  background: linear-gradient(135deg, #f5f7fa 0%, #e4e8f0 100%) fixed;
  min-height: 100vh;
  width: 100vw;
  margin: 0;
  padding: 0;
}
.role-selector-container {
  max-width: none;
  min-height: 100vh;
  width: 100vw;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  position: relative;
  overflow: hidden;
  background: none;
}
.content-wrapper {
  position: relative;
  z-index: 1;
  max-width: 900px;
  padding: 40px 30px 30px 30px;
  text-align: center;
  margin: 0 auto;
  background: none;
  border-radius: 0;
  box-shadow: none;
}

.dynamic-background {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 0;
  overflow: hidden;
  pointer-events: none;
}

.floating-item {
  position: absolute;
  will-change: transform;
  animation: float linear infinite;
  z-index: 0;
}

@keyframes float {
  0% {
    transform: translateY(0) rotate(0deg);
  }
  50% {
    transform: translateY(-20px) rotate(5deg);
  }
  100% {
    transform: translateY(0) rotate(0deg);
  }
}

.header {
  margin-bottom: 60px;
  position: relative;
  z-index: 1;
}

.header h2 {
  font-size: 32px;
  color: #303133;
  margin-bottom: 15px;
  font-weight: 600;
  text-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.header p {
  font-size: 16px;
  color: #606266;
  letter-spacing: 0.5px;
}

.role-cards {
  display: flex;
  justify-content: center;
  gap: 40px;
  margin-bottom: 50px;
  position: relative;
  z-index: 1;
  flex-wrap: wrap;
}

.role-card {
  width: 300px;
  height: 280px;
  cursor: pointer;
  transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
  border: 2px solid transparent;
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(5px);
  position: relative;
  overflow: hidden;
}

.role-card:hover {
  transform: translateY(-8px) scale(1.02);
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.1) !important;
}

.role-card.active {
  border-color: #409EFF;
  box-shadow: 0 8px 25px rgba(64, 158, 255, 0.2) !important;
}

.role-card.active .highlight-bar {
  width: 100%;
}

.card-content {
  padding: 30px;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100%;
  position: relative;
}

.icon-wrapper {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 25px;
  transition: all 0.3s;
}

.icon-wrapper.merchant {
  background: rgba(64, 158, 255, 0.1);
  color: #409EFF;
}

.icon-wrapper.customer {
  background: rgba(103, 194, 58, 0.1);
  color: #67C23A;
}

.role-card.active .icon-wrapper.merchant {
  background: rgba(64, 158, 255, 0.2);
  box-shadow: 0 0 0 8px rgba(64, 158, 255, 0.1);
}

.role-card.active .icon-wrapper.customer {
  background: rgba(103, 194, 58, 0.2);
  box-shadow: 0 0 0 8px rgba(103, 194, 58, 0.1);
}

.card-content h3 {
  margin: 0 0 15px;
  font-size: 20px;
  color: #303133;
  font-weight: 500;
}

.card-content p {
  font-size: 14px;
  color: #606266;
  line-height: 1.6;
  margin-bottom: 20px;
}

.highlight-bar {
  position: absolute;
  bottom: 0;
  left: 0;
  height: 4px;
  width: 0;
  background: linear-gradient(90deg, #409EFF, #67C23A);
  transition: width 0.3s ease;
}

.confirm-btn {
  width: 220px;
  height: 50px;
  font-size: 16px;
  border-radius: 25px;
  position: relative;
  z-index: 1;
  transition: all 0.3s;
  box-shadow: 0 4px 15px rgba(64, 158, 255, 0.3);
  overflow: hidden;
}

.confirm-btn:hover {
  transform: translateY(-3px);
  box-shadow: 0 6px 20px rgba(64, 158, 255, 0.4);
}

.confirm-btn:active {
  transform: translateY(1px);
}

.confirm-btn .arrow-right {
  margin-left: 8px;
  transition: transform 0.3s;
}

.confirm-btn:hover .arrow-right {
  transform: translateX(3px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .role-selector-container {
    padding: 20px;
  }

  .role-cards {
    flex-direction: column;
    align-items: center;
    gap: 20px;
  }

  .role-card {
    width: 100%;
    max-width: 350px;
  }

  .header h2 {
    font-size: 26px;
  }
}
</style>